package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc/reflection"
	"os"
	srv "project-common"
	_ "project-user/api"
	"project-user/config"
	"project-user/pkg/svc"
	"project-user/router"
	"strings"
)

func main() {
	// 获取命令行参数
	args := os.Args
	var envs = make(map[string]interface{})
	// 遍历并打印命令行参数
	for i, arg := range args {
		params := strings.Split(arg, "=")
		if len(params) == 2 {
			switch params[0] {
			case "-env":
				// env = params[1]
				envs["env"] = params[1]
			case "-addr":
				envs["addr"] = params[1]
			case "-grpcaddr":
				envs["grpcaddr"] = params[1]

			}
		}
		fmt.Printf("参数 %d: %s\n", i, arg)
	}
	svcCtx := svc.NewServiceContext(config.C)

	r := gin.Default()
	// 路由
	router.InitRouter(r)
	grpcaddr, ok := envs["grpcaddr"]
	if ok {
		config.C.GC.Addr = grpcaddr.(string)
	}
	// grpc服务注册
	grpcServer := router.RegisterGrpc(svcCtx)

	// 注册反射服务
	reflection.Register(grpcServer)

	stopGrpcServer := func() {
		grpcServer.Stop()
	}

	// grpc服务注册到etcd
	router.RegisterEtcdServer()

	addr, ok := envs["addr"]
	if !ok {
		addr = config.C.SC.Addr
	}

	srv.Run(r, config.C.SC.Name, addr.(string), stopGrpcServer)
}
